package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import com.financing.platform.service.IFpProjectTransferHisService;
import com.financing.platform.service.IUserInfoService;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.dto.FpProjectTransferHisDTO;
import com.financing.platform.dto.UserInfoDTO;

/**
 * @Description: Controller
 * @author caojiayao
 * @version 1.0
 * @created 
 */
@Controller
@RequestMapping("/api/product")
@Scope("prototype")
public class FpProjectTransferHisController  extends BaseAction {
	@Autowired
	private IFpProjectTransferHisService fpProjectTransferHisService;
	
	@Autowired
	private IUserInfoService userInfoService;
	
	@RequestMapping("/transferHisQueryList.shtml")
	@ResponseBody
	public ResultMap queryList(HttpServletRequest request,
			HttpServletResponse response,FpProjectTransferHisDTO dto,Pager pager) {
		try{
			log.info("Begin query fa user list!");
			
			dto.setBegin(pager.getStartIndex());
			dto.setPageSize(pager.getPageSize());
			if(dto.getpProjectType() == null){
				//01：信托，资管
				dto.setpProjectType("01");
			}
			
			int count = fpProjectTransferHisService.countFpProjectTransferHis(dto);
			pager.setTotalCount(count);
			List<FpProjectTransferHisDTO> list = fpProjectTransferHisService.listFpProjectTransferHis(dto);
			
			for(FpProjectTransferHisDTO rDto : list){
				rDto.setFormUserName(fpProjectTransferHisService.getUserName(rDto.getFormUserId()));
				rDto.setToUserName(fpProjectTransferHisService.getUserName(rDto.getToUserId()));
			}
			
			
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End querylist!");
		}catch(Exception e){
			log.error("queryList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("/transferHisAdd.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response,FpProjectTransferHisDTO dto) {
		try{
			log.info("Begin add info !");
			
			DTOdateSet.setCreateInfo(dto, request.getSession());
			
			UserInfoDTO userInfoDTO = new UserInfoDTO();
			userInfoDTO.setUserId(dto.getToUserId());
			if(dto.getpProjectType() == null){
				//01：信托，资管
				dto.setpProjectType("01");
			}
			if(userInfoService.queryUserInfo(userInfoDTO) == null){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				resultMap.setMessage("不存在被转移人UserID!");
				return resultMap;
			}
			
			//获得权限转移人ID
			if(null != request.getSession() && null != request.getSession().getAttribute(DTOdateSet.user)){
				UserInfoDTO userinfo = (UserInfoDTO) request.getSession().getAttribute(DTOdateSet.user);
				dto.setFormUserId(userinfo.getUserId());
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				resultMap.setMessage("未获取到转移人信息!");
				return resultMap;
			}
			
			Integer id =fpProjectTransferHisService.insertFpProjectTransferHis(dto);
			
			resultMap.getAttribute().put("id",id);
			
			if(id !=null && id >0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End add fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	
}